'use strict'

var Koa = require('koa')
var config = require('./config')
var env = process.env.NODE_ENV || 'development'

//var weixin = require('./weixin')
//var wechat = require('./wechat/generator')

var User = require('./app/schemas/user.js')

var Wechat = require('./wechat/wechat') 

var path = require('path')

var port = process.env.PORT || 8081
var fs = require('fs')

// models loading  //req.params.userId req.query.userId req.body.userId       req.param.userId
// var models_path = __dirname + '/app/models'
// var walk = function(path) {
//   fs
//     .readdirSync(path)
//     .forEach(function(file) {
//       var newPath = path + '/' + file
//       var stat = fs.statSync(newPath)

//       if (stat.isFile()) {
//         if (/(.*)\.(js|coffee)/.test(file)) {
//           require(newPath)
//         }
//       }
//       else if (stat.isDirectory()) {
//         walk(newPath)
//       }
//     })
// }
// walk(models_path)

var app = new Koa()
var Router = require('koa-router')
var session = require('koa-session')
var kaostatci = require('koa-static')
var bodyParser = require('koa-bodyparser')
var views = require('koa-views')
var moment = require('moment')
// var router = new Router()
// require('./config/routes')(router)
app.use(kaostatci(__dirname+'/static'));
app.use(views(__dirname+'/app/views',{
  default:'jade',
  // locals: {
  //   moment: moment
  // },
  options: {
      helpers: {
        moment: moment
      }
    }
}))
// app.use(function*(next){
//   var fn = require('connect-livereload')()
//   fn(this.req,this.res)
//   yield next
// });
app.keys = ['wechat']
app.use(session(app))
app.use(bodyParser())
app.use(function* (next){ 
	var user = this.session.user
	if(user && user._id){
		// this.session.user = yield User.findOne({_id:user._id}).exec()
		this.locals.user = this.session.user
	}else{
		this.locals.uer = null
	}

	yield next
})

// app
//    .use(router.routes())
//    .use(router.allowedMethods())




app.listen(3000)

console.log('Listening 3000...')












//安装bcrpt等未安装   ,路由改为koa-router